/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.mcx.modules.rest;

import com.mcx.exception.BadRequestException;
import com.mcx.exception.handler.ApiSuccess;
import com.mcx.modules.domain.Channel;
import com.mcx.modules.service.ChannelService;
import com.mcx.modules.service.dto.ChannelDto;
import com.mcx.modules.service.dto.ChannelQueryCriteria;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
* @website https://el-admin.vip
* @author mcx
* @date 2022-04-30
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "channel管理")
@RequestMapping("/api/channel")
public class ChannelController {

    private final ChannelService channelService;

    @GetMapping
    public ResponseEntity<Object> queryChannel(ChannelQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(channelService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    public ResponseEntity<Object> createChannel(@Validated @RequestBody Channel resources){
        return new ResponseEntity<>(channelService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    public ResponseEntity<Object> updateChannel(@Validated @RequestBody Channel resources){
        channelService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    public ResponseEntity<Object> deleteChannel(@RequestBody Integer[] ids) {
        channelService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/init")
    public ResponseEntity<Object> init(@Validated @RequestBody ChannelQueryCriteria criteria){
        ChannelDto channelDto = channelService.findByChannelIdAndInnerVersion(criteria.getChannelId(), criteria.getInnerVersion());
        if (channelDto!=null) return ResponseEntity.ok(ApiSuccess.response(channelDto, "获取渠道信息成功"));
        else throw new BadRequestException("渠道号不存在");
    }
}